package com.example.controller;

import com.example.service.ProductsService;
import com.example.common.Result;
import com.example.entity.Products;
import com.example.entity.Params;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/products")
public class ProductsController {

    private static final Logger log = LoggerFactory.getLogger(ProductsController.class);

    @Resource
    private ProductsService productsService;

    @GetMapping
    public Result findAll() {
        List<Products> list = productsService.findAll();
        return Result.success(list);
    }

    @GetMapping("/search")
    public Result findBySearch(Params params) {
        log.info("调用接口查询商品信息");
        PageInfo<Products> info = productsService.findBySearch(params);
        return Result.success(info);
    }

    @PostMapping
    public Result save(@RequestBody Products product) {
        if (product.getProductId() == null) {
            productsService.add(product);
        } else {
            productsService.update(product);
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer productId) {
        productsService.delete(productId);
        return Result.success();
    }
}
